/* @(#)GSSystemConfigurator.java
 *
 * Project: BUS
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   Janus Liu    2005-12-22 建立
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.lib;

import java.util.Enumeration;
import java.util.MissingResourceException;
import java.util.Properties;
import java.util.ResourceBundle;
import org.apache.log4j.PropertyConfigurator;

/**
 * @author simonyi
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class GSSystemConfigurator {
    

    private static final String RESOURCE_NAME = "gs";

    private static boolean iIsInitialed = false;

    private static ResourceBundle iResourceBundle = null;
    
    public static String RUNTIME_MODE_UNIT_TEST_CODE = "";
    
    private static String iDataSourceJNDI = "";
    
    private static Properties sLog4jProperties = null;
    
    private static GSSystemConfigurator iGSSystemConfigurator = null;

    private GSSystemConfigurator() throws Exception {
        //读取系统配置文件
        bundle();
    }
    public static GSSystemConfigurator getInstance() throws Exception{
    	if(iGSSystemConfigurator == null){
    		iGSSystemConfigurator = new GSSystemConfigurator();
    	}
    	//建立上载路径
    	//java.io.File uploadFile = new java.io.File(GSBaseConstants.UPLOADPATH);
    	//if(!uploadFile.exists())
    	//    uploadFile.createNewFile();
    	
    	return iGSSystemConfigurator;
    }
   
    private void bundle() throws Exception {
	    if (!iIsInitialed) {
	        try {
//	        	String tConfigPath = System.getProperty("gs.config.path");
//	            if (tConfigPath == null)
//	                throw new MissingResourceException("未设定系统配置参数路径[gs.config.path]", "GSSystemConfigurator", "gs.config.path");
	            iResourceBundle = ResourceBundle.getBundle(RESOURCE_NAME);
	            iIsInitialed = true;
	        }
	        catch (Exception e) {
	            e.printStackTrace();
	            throw new Exception("无法读取系统配置文件配置信息");
	        }
	        
//	        iDataSourceJNDI = iResourceBundle.getString("DataSourceJNDI").trim();
//            if (iDataSourceJNDI.length() == 0) {
//                throw new Exception("数据源配置错误");
//            }
            //3、初始化系统日志对象
            try {
                //3.1 日志参数配置在应用系统配置文件中。
                PropertyConfigurator.configure(getLog4jProerties());
            }
            catch (Exception e) {
                throw new Exception("无法初始化系统日志对象");
            }
	    }	    
    }
    
    public static String getJNDI() throws Exception {
//        bundle();
        return iDataSourceJNDI;
    }
    
    public static Properties getLog4jProerties() {
        try{
//            bundle();
            if (sLog4jProperties == null){
                Properties tProperties = new Properties();
                Enumeration tEnumeration = iResourceBundle.getKeys();
                while (tEnumeration.hasMoreElements()) {
                    String tKey = (String)tEnumeration.nextElement();
                    if (tKey.startsWith("log4j")){
                        tProperties.put(tKey, iResourceBundle.getString(tKey));
                    }
                }
                sLog4jProperties = tProperties;
            }            
        }catch(Exception e){
        	e.printStackTrace();
        }
        return sLog4jProperties;
    }
    public static String getParam(String aParamName) throws MissingResourceException {
        if (!iIsInitialed) {
            throw new MissingResourceException("系统参数解析类未初始化", "GSSystemConfigurator", "");
        }

        String tValue = null;
        if (aParamName.indexOf("gs.") == 0) {
            try {
                tValue = iResourceBundle.getString(aParamName);
            }
            catch (Exception e) { }
        }
        
        return tValue;
    }
    
    public static String [] getParams(String aParamName) throws MissingResourceException {
        if (!iIsInitialed) {
            throw new MissingResourceException("系统参数解析类未初始化", "GSSystemConfigurator", "");
        }

        String  value = null;
        if (aParamName.indexOf("gs.") == 0) {
            try {
               value = iResourceBundle.getString(aParamName);
               return value.split(",");
                
            }
            catch (Exception e) { }
        }
        
        return null;
    }
    
    //项目所属分站
    public static String getProjectVersion() throws MissingResourceException 
    {
        if (!iIsInitialed) 
        {
            throw new MissingResourceException("系统参数解析类未初始化", "GSSystemConfigurator", "");
        }

        String tValue = null;
        try 
        {
            tValue = iResourceBundle.getString("gs.Version");
        }
        catch (Exception e) { }
        
        return tValue;
    }
    //系统版本
    public static String getProjectVersionType() throws MissingResourceException 
    {
        if (!iIsInitialed) 
        {
            throw new MissingResourceException("系统参数解析类未初始化", "GSSystemConfigurator", "");
        }

        String tValue = null;
        try 
        {
            tValue = iResourceBundle.getString("gs.Version.type");
        }
        catch (Exception e) { }
        
        return tValue;
    }
}